public class MyLinkedList implements IList{

    static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }
    }
    ListNode head;

   public void createNode(){
       ListNode node1=new ListNode(12);
       ListNode node2=new ListNode(23);
       ListNode node3=new ListNode(34);
       ListNode node4=new ListNode(45);
       ListNode node5=new ListNode(56);

       //通过next串联节点
       node1.next=node2;
       node2.next=node3;
       node3.next=node4;
       node4.next=node5;


       this.head=node1;

   }





    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
        }else {
          node.next=head;
          head=node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node=new ListNode(data);
        ListNode cur=this.head;
        if(head==null){
            head=node;
        }else {
            while (cur.next!=null){
                cur=cur.next;
            }
            cur.next=node;
        }

    }
    public ListNode searchPrev(int index){
       ListNode cur=head;
       while (index-1!=0){
           cur=cur.next;
           index--;
       }
       return cur;
    }
    public void IndexAbnormal(int index){
       if (index<0||index>size()){
           throw new IndexAbnormity("index元素异常");
       }
    }
    @Override
    public void addIndex(int index, int data) {
       try {
           IndexAbnormal(index);
       }catch (IndexAbnormity e){
           e.printStackTrace();
       }
       ListNode newH=new ListNode(data);
       ListNode cur=searchPrev(index);
       if(index==0){
           addFirst(data);
       }
       if (index==size()){
            addLast(data);
            return;
       }

        newH.next=cur.next;
        cur.next=newH;


    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //找到要删除的节点
    private ListNode FindPrev(int key){
        ListNode cur =head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
        }
        return null;
    }


    @Override
    public void remove(int key) {
        //1.如果为空,删除不了
        if(head==null){
            System.out.println("链表为空无法删除");
        }
        //2.删除节点为头节点
        if (head.val==key){
            this.head=head.next;
        }
        ListNode cur=FindPrev(key);
        ListNode del=cur.next;
        cur.next=del.next;

    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        int count=0;
        ListNode cur=head;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        this.head=null;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
